
/**
 * 摄像头 
 */
export class Camera {
    /**
     * @param { Number } width 摄像头容器宽
     * @param { Number } height 摄像头容器高
     * @param { Number } cameraWidth 摄像头宽
     * @param { Number } cameraHeight 摄像头高
     */
    constructor(width, height, cameraWidth, cameraHeight) {
        /* 摄像头容器宽 */
        this.width = width;
        /* 摄像头容器高 */
        this.height = height;
        /* 摄像头宽 */
        this.cameraWidth = cameraWidth;
        /* 摄像头高 */
        this.cameraHeight = cameraHeight;
        /* 摄像头参数 */
        this.cameraParam = { audio: false, video: { facingMode: "environment", width: this.cameraHeight, height: this.cameraWidth, } };
        /* 摄像头容器 */
        this.videoEl = null
        /* 离屏canvas */
        this.canvasEl = null
        this.canvasCtx = null
        this.createVideo()
        this.createCanvas()
    }
    /**
     * 打开摄像头
     */
    openCamera() {
        // 如果已打开摄像头，则需要先关闭。
        this.closeCamera();
        return new Promise((resolve, reject) => {
            if (!navigator.mediaDevices || !navigator.mediaDevices.getUserMedia) {
                reject(new Error("navigator.mediaDevices.getUserMedia is undefined"));
                return
            }
            navigator.mediaDevices.getUserMedia(this.cameraParam).then(stream => {
                this.videoEl.srcObject = stream;
                this.videoEl.style.display = 'block';
                this.videoEl.play().then((res) => {
                    console.info(`摄像头视频绑定成功`);
                }).catch(err => {
                    console.error(`摄像头视频绑定失败:\n${err}`);
                    reject(err);
                });
                this.videoEl.onloadedmetadata = () => {
                    resolve(true);
                };
            }).catch(err => {
                reject(err);
            });
        });
    };
    /**
     * 关闭摄像头 
     */
    closeCamera() {
        if (this.videoEl && this.videoEl.srcObject) {
            this.videoEl.srcObject.getTracks().forEach(track => {
                console.info('stop camera');
                track.stop();
            });
        }
    };
    /**
     * 创建摄像头 
     */
    createVideo() {
        if (this.videoEl) return
        this.videoEl = document.createElement('video');
        this.videoEl.setAttribute('playsinline', 'playsinline');
        this.videoEl.setAttribute('width', `${this.width}px`);
        this.videoEl.setAttribute('height', `${this.height}px`);
        this.videoEl.setAttribute('id', 'arVideo');
        this.videoEl.setAttribute('style', `display:none;position: fixed; left: ${(window.innerWidth - this.width) / 2}px;top: ${(window.innerHeight - this.height) / 2}px;z-index: -2;width:${this.width}px;height:${this.height}px`);
        document.body.appendChild(this.videoEl);
    }
    /**
     * 创建canvas，截取摄像头图片时使用
     */
    createCanvas() {
        this.canvasEl = document.createElement('canvas');
        this.canvasEl.setAttribute('width', this.cameraWidth);
        this.canvasEl.setAttribute('height', this.cameraHeight);
        this.canvasCtx = this.canvasEl.getContext('2d');
    };
    /**
     * 截取摄像头图片
     */
    captureVideo() {
        if (!this.videoEl) return ""
        this.canvasCtx.drawImage(this.videoEl, 0, 0, this.videoEl.offsetWidth, this.videoEl.offsetHeight);
        return this.canvasEl.toDataURL('image/jpeg', 0.5);
    };
}